local skel = fk.CreateSkill{
  name = "tea__fangzi",
  tags = { Skill.Switch },
}
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["tea__fangzi"] = "放恣",
  [":tea__fangzi"] = "转换技，每回合限一次，你可以将①一②三张牌当【酒】或【无中生有】使用，然后你须与一名手牌数与你相同的角色交换手牌。",
  ["#tea__fangzi-swap"] = "放恣：选择一名角色，与其交换手牌",
  ["#tea__fangzi-yang"] = "放恣：①将一张牌当【酒】或【无中生有】使用",
  ["#tea__fangzi-yin"] = "放恣：②将三张牌当【酒】或【无中生有】使用",
}
---@type ViewAsSkillSpec
local fangzi = {
  mute = true,
  pattern = "analeptic,ex_nihilo",
  prompt = function (self, player, selected_cards, selected)
    if player:getSwitchSkillState(skel.name, false) == fk.SwitchYang then
      return "#tea__fangzi-yang"
    else
      return "#tea__fangzi-yin"
    end
  end,
  interaction = function (self, player)
    local all_names = { "analeptic", "ex_nihilo" }
    local names = player:getViewAsCardNames(skel.name, all_names, player:getCardIds("he"))
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  card_filter = function (self, player, to_select, selected)
    if player:getSwitchSkillState(skel.name, false) == fk.SwitchYang then
      return #selected == 0
    else
      return #selected < 3
    end
  end,
  view_as = function (self, player, cards)
    if player:getSwitchSkillState(skel.name, false) == fk.SwitchYang then
      if #cards ~= 1 or not self.interaction.data then return end
    else
      if #cards ~= 3 or not self.interaction.data then return end
    end
    local c = Fk:cloneCard(self.interaction.data)
    c:addSubcards(cards)
    c.skillName = skel.name
    return c
  end,
  before_use = function (self, player, use)
    local room = player.room
    room:notifySkillInvoked(player, skel.name, "switch")
    local num = table.indexOf({ "analeptic", "ex_nihilo" }, self.interaction.data) or -1
    player:broadcastSkillInvoke(skel.name, num)
  end,
  after_use = function (self, player, use)
    local room = player.room
    local tos = table.filter(room:getOtherPlayers(player), function (p)
      return #player:getCardIds("h") == #p:getCardIds("h")
    end)
    if #tos > 0 then
      local to = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        cancelable = false,
        prompt = "#tea__fangzi-swap"
      })
      if #to == 1 then
        room:swapAllCards(player, { player, to[1] }, skel.name, "h")
      end
    end
  end,
  enabled_at_play = function (self, player)
    local card
    if player:getSwitchSkillState(skel.name, false) == fk.SwitchYang then
      card = Fk:cloneCard("analeptic")
    else
      card = Fk:cloneCard("ex_nihilo")
    end
    return player:canUse(card) and player:usedSkillTimes(skel.name, Player.HistoryRound) == 0
  end,
  enabled_at_response = function (self, player, response)
    if not response and player:usedSkillTimes(skel.name, Player.HistoryRound) == 0 then
      local card
      if player:getSwitchSkillState(skel.name, false) == fk.SwitchYang then
        card = Fk:cloneCard("analeptic")
      else
        card = Fk:cloneCard("ex_nihilo")
      end
      return not Fk.currentResponsePattern or Exppattern:Parse(Fk.currentResponsePattern):match(card)
    end
    return false
  end
}
skel:addEffect("viewas", fangzi)
return skel